#####################################################################
## Replication file for                                           
## When Do Citizens Consider Political Parties Legitimate?  
##                                   
## Author: Ann-Kristin Kölln (ann-kristin.kolln@gu.se)                                                     
## 26th May 2023                                                 
##                
#####################################################################


# R version 4.3.0 (2023-04-21) -- "Already Tomorrow"; Platform: x86_64-apple-darwin20 (64-bit)


## load pilot survey data for experiment
data<-read.csv2("PartyLeg_pilot.csv", header= TRUE, sep=",")


#install and load required packages
install.packages("plyr")

library(plyr)


### ideology treatment

data$treat_ideol <-as.factor(data$treat_ideol)
data$treat_ideol <- factor(data$treat_ideol, levels = c("control", "left-wing", "centre-left", "centre", "centre-right", "right-wing"))

## producing Table E1
ddply(data, ~treat_ideol, summarise, mean = mean(PercIdeol_a, na.rm=TRUE), sd = sd(PercIdeol_a, na.rm = TRUE), N = length(PercIdeol_a))

fit_ideol_a<- aov(PercIdeol_a ~ treat_ideol, data= data)
summary(fit_ideol_a)

TukeyHSD(fit_ideol_a)


## producing Table E2
ddply(data, ~treat_ideol, summarise, mean = mean(PercIdeol_b, na.rm=TRUE), sd = sd(PercIdeol_b, na.rm = TRUE), N = length(PercIdeol_b))

fit_ideol_b<- aov(PercIdeol_b ~ treat_ideol, data= data)
summary(fit_ideol_b)

TukeyHSD(fit_ideol_b)


## producing Table E3
ddply(data, ~treat_ideol, summarise, mean = mean(PercIdeol_c, na.rm=TRUE), sd = sd(PercIdeol_c, na.rm = TRUE), N = length(PercIdeol_c))

fit_ideol_c<- aov(PercIdeol_c ~ treat_ideol, data= data)
summary(fit_ideol_c)
TukeyHSD(fit_ideol_c)


## producing Table E4
ddply(data, ~treat_ideol, summarise, mean = mean(PercIdeol_d, na.rm=TRUE), sd = sd(PercIdeol_d, na.rm = TRUE), N = length(PercIdeol_d))

fit_ideol_d<- aov(PercIdeol_d ~ treat_ideol, data= data)
summary(fit_ideol_d)

TukeyHSD(fit_ideol_d)



### democratic behavior treatment

data$treat_demo <-as.factor(data$treat_demo)
data$treat_demo <- factor(data$treat_demo, levels = c("control", "pro-democratic", "anti-democratic"))


## producing Table E5
ddply(data, ~treat_demo, summarise, mean = mean(PercDemo_a, na.rm=TRUE), sd = sd(PercDemo_a, na.rm = TRUE), N = length(PercDemo_a))

fit_demo_a<- aov(PercDemo_a ~ treat_demo, data= data)
summary(fit_demo_a)

TukeyHSD(fit_demo_a)


## producing Table E6
ddply(data, ~ treat_demo, summarise, mean = mean(PercDemo_b, na.rm=TRUE), sd = sd(PercDemo_b, na.rm = TRUE), N = length(PercDemo_b))

fit_demo_b <- aov(PercDemo_b ~ treat_demo, data= data)
summary(fit_demo_b)

TukeyHSD(fit_demo_b)


## producing Table E7
ddply(data, ~ treat_demo, summarise, mean = mean(PercDemo_c, na.rm=TRUE), sd = sd(PercDemo_c, na.rm = TRUE), N = length(PercDemo_c))

fit_demo_c <- aov(PercDemo_c ~ treat_demo, data= data)
summary(fit_demo_c)


## producing Table E8
ddply(data, ~ treat_demo, summarise, mean = mean(PercDemo_d, na.rm=TRUE), sd = sd(PercDemo_d, na.rm = TRUE), N = length(PercDemo_d))

fit_demo_d <- aov(PercDemo_d ~ treat_demo, data= data)
summary(fit_demo_d)



### ideology treatment affects perception of democratic attitudes?
fit_plac_a <- aov(PercDemo_a ~ treat_ideol, data= data)
summary(fit_plac_a)

fit_plac_b <- aov(PercDemo_b ~ treat_ideol, data= data)
summary(fit_plac_b)

fit_plac_c <- aov(PercDemo_c ~ treat_ideol, data= data)
summary(fit_plac_c)

fit_plac_d <- aov(PercDemo_d ~ treat_ideol, data= data)
summary(fit_plac_d)

